from flask import Flask, render_template, request, jsonify
from DAL import MovieTypeDAL, MovieDAL
from Model import MovieType, Movie

mtdal=MovieTypeDAL()
mdal=MovieDAL()
app = Flask(__name__)

@app.route("/")
def index():
    res=mdal.showBytitle("")
    return render_template('home.html',info=res)

@app.route("/searchMovie",methods=["POST"])
def searchMovie():
    title=request.form.get("title")
    res=mdal.showBytitle(title)
    return render_template('home.html',info=res,search_title=title)



@app.route("/list")
def list():
    res=mtdal.showall()
    return render_template('list.html',info=res)

@app.route("/add")
def add():
    return render_template('add.html')

@app.route("/addSubmit",methods=["POST"])
def addSubmit():
    try:
        tid=int(request.form.get("tid"))
        if tid<=0:
            raise Exception("编号必须为正整数！")
        exist=mtdal.showBytid(tid)
        if not exist:
            tname=request.form.get("tname")
            tcontent=request.form.get("tcontent")
            mt=MovieType(tid,tname,tcontent)
            res=mtdal.insert(mt)
            if res>0:
                return "录入成功 <br><a href='/list'>刷新</a>"
            elif res==0:
                return "录入失败 <br><a href='/add'>刷新</a>"
            elif res==-1:
                return "异常错误 <br><a href='/add'>刷新</a>"
        else:
            return "录入失败，该类型编号已存在！ <br><a href='/add'>刷新</a>"
    except Exception as e:
        return f"{e}<br><a href='/add'>刷新</a>"
    
@app.route("/update/<tid>")
def update(tid):
    res=mtdal.showBytid(tid)
    return render_template('update.html',info=res)

@app.route("/updateSubmit",methods=["POST"])
def updateSubmit():
    tid=request.form.get("tid")
    tname=request.form.get("tname")
    tcontent=request.form.get("tcontent")
    mt=MovieType(tid,tname,tcontent)
    res=mtdal.update(mt)
    if res[0]>0:
        edname="名称已修改"
    else:
        edname="名称未修改"
    if res[1]>0:
        edcontent="内容已修改"
    else:
        edcontent="内容未修改"
    return f"{edname}<br>{edcontent}<br><a href='/list'>刷新</a>"
    
@app.route("/delete/<tid>")
def delete(tid):
    res=mtdal.delet(tid)
    if res>0:
        return "删除成功 <br><a href='/list'>刷新</a>"
    else:
        return "删除失败 <br><a href='/list'>刷新</a>"
    
@app.route("/movieList/<tid>")
def movieList(tid):
    res=mdal.showBytid(tid)
    return render_template('movieList.html',info=res)

@app.route("/addMovie")
def addMovie():
    res=mtdal.showall()
    return render_template('addMovie.html',info=res)

@app.route("/addMovieSubmit",methods=["POST"])
def addMovieSubmit():
    try:
        id=int(request.form.get("id"))
        if id<=0:
            raise Exception("编号必须为正整数！")
        title=request.form.get("title")
        mdate=request.form.get("mdate")
        score=request.form.get("score")
        tid=request.form.get("tid")
        exist=mdal.showByid(id)
        if not exist:
            m=Movie(id,title,mdate,score,tid)
            res=mdal.insert(m)
            if res>0:
                return f"添加成功 <br><a href='/movieList/{tid}'>刷新</a>"
            else:
                return f"添加失败 <br><a href='/addMovie'>刷新</a>"
        else:
            return f"录入失败！该电影编号已存在：<br>{exist}<br><a href='/addMovie'>刷新</a>"
    except Exception as e:
        return f"{e}<br><a href='/addMovie'>刷新</a>"

@app.route("/updateMovie/<id>")
def updateMovie(id):
    res=mtdal.showall()
    res1=mdal.showByid(id)
    return render_template("updateMovie.html",info=res,movie=res1)

@app.route("/updateMovieSubmit",methods=["POST"])
def updateMovieSubmit():
    id=request.form.get("id")
    title=request.form.get("title")
    mdate=request.form.get("mdate")
    score=request.form.get("score")
    tid=request.form.get("tid")
    m=Movie(id,title,mdate,score,tid)
    res=mdal.update(m)
    if res>0:
        return f"修改成功<br><a href='/movieList/{tid}'>刷新</a>"
    elif res==0:
        return f"未进行修改<br><a href='/movieList/{tid}'>刷新</a>"
    elif res==-1:
        return f"异常错误 <br><a href='/movieList/{tid}'>刷新</a>"


@app.route("/deleteMovie/<id>")
def deleteMovie(id):
    movie=mdal.showByid(id)
    res=mdal.delete(id)
    if res>0:
        return f"删除成功 <br><a href='/movieList/{movie[4]}'>刷新</a>"
    else:
        return f"删除失败 <br><a href='/movieList/{movie[4]}'>刷新</a>"
    
@app.route("/dataView")
def dataView():
    return render_template("dataView.html")

@app.route("/zhuData")
def zhuData():
    #准备json数据
    zhudata=mdal.getMTCount()
    return jsonify(zhudata)

@app.route("/bing")
def bing():
    return render_template("bing.html")

@app.route("/bingData")
def bingData():
    #准备json数据
    res=mdal.getMTCount()
    bingdata=[{"value":i[1], "name":i[0]} for i in res]
    return jsonify(bingdata)

@app.route("/scoreData")
def scoreData():
    #准备json数据
    scoredata=mdal.getMTScore()
    return jsonify(scoredata)

if __name__=="__main__":
    app.run(host="127.0.0.1",port=5000,debug=True)